package com.staginfo.segs.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.staginfo.segs.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by robert.luozizhao on 2017/9/12 0012.
 */

public class ManagerPage extends RelativeLayout {


    @Bind(R.id.grid_view)
    GridView gridView;
    private List<Integer> mListImag = new ArrayList();
    private List<String> mTitel = new ArrayList();

    private String[] mResTitle = {"初始化设备", "设备轨迹", "设备基本信息",
            "初始化设备", "设备轨迹", "设备基本信息",
            "初始化设备", "设备轨迹", "设备基本信息"};
    private Integer[] mResImagId = {R.drawable.manager_devinit,R.drawable.manager_historytrack,R.drawable.manager_devinit,
            R.drawable.manager_devinit,R.drawable.manager_historytrack,R.drawable.manager_devinit,
            R.drawable.manager_devinit,R.drawable.manager_historytrack,R.drawable.manager_devinit};


    public ManagerPage(Context context) {
        this(context, null);
    }

    public ManagerPage(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switchItemClick(position);
            }
        });
    }

    private void switchItemClick(int position) {

        switch (position){
            case 0:
                Toast.makeText(getContext(),mResTitle[position],Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(getContext(),mResTitle[position],Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(getContext(),mResTitle[position],Toast.LENGTH_SHORT).show();
                break;
        }


    }

    private void initData() {
        for (int i = 0; i < mResTitle.length; i++) {
            mListImag.add(mResImagId[i]);
            mTitel.add(mResTitle[i]);
        }
    }


    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.page_manger, this);
        ButterKnife.bind(this, this);
        gridView.setAdapter(mManagerPageAdapter);
    }


    public BaseAdapter mManagerPageAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return mResTitle.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_view_manager, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //刷新标题和图片
            viewHolder.ivImg.setImageDrawable(convertView.getResources().getDrawable(mListImag.get(position)));
            viewHolder.tvTitle.setText(mTitel.get(position).toString());
            return convertView;
        }



    };


    public class ViewHolder {
        ImageView ivImg;
        TextView tvTitle;

        public ViewHolder(View root) {
            ivImg = (ImageView) root.findViewById(R.id.iv_img);
            tvTitle = (TextView) root.findViewById(R.id.tv_title);
        }

    }




}
